#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "alg.h"

//--quiet
//int stil = 0 ;
//// (default)aantal dagen om te grabben: --days
//int days = 7 ;
//// --offset
//int offset = 0;
//// --cache
//short cache = 0;
extern FILE *cachefp;
extern FILE *cachetmp;
extern unsigned long aantalcache;
extern pthread_mutex_t mymutex;
//// systeem jaar, maand, dag.
extern int sys_jaar;
extern int sys_maand;
extern int sys_dag;

// opzoeken id in cache, return pointer to position else -1
/* cache:
 * 
 * tvgids: 0"ID=xxxxx"-"startdatum"-"titel"alles
 * upc: 1"kanaalid"-"startdatum"alles
 * sbs: 2"kanaalid"-"startdatumtijd"alles
 * Alle xml cachen, is makkelijkste en levert geen problemen met
 * toekomstige toevoegingen/veranderingen.
*/


long vind_cache(char *id, char provider)
{
	int len = strlen(id);
	char buffer[1023];
	char *p;
	long pos;

	pthread_mutex_lock(&mymutex);
	rewind(cachefp);
	pos = ftell(cachefp);
	while (fgets(buffer, 1023, cachefp)) {
		if (buffer[0] == '' && buffer[1] == provider) {
			p = strchr(buffer + 3, '');
			if (!p)
				continue;	//corruptie in cache
			*p = '\0';
			if (!strncmp(id, buffer + 3, len)) {
				pthread_mutex_unlock(&mymutex);
				return (pos);
			}
		}
		pos = ftell(cachefp);
	}
	pthread_mutex_unlock(&mymutex);
	return (-1);

}

// cache entry display voor xmloutput
void printcache(long pos)
{
	char buffer[1024];
	char *p, *p2;
	pthread_mutex_lock(&mymutex);
	if (fseek(cachefp, pos, SEEK_SET) == 0) {
		fgets(buffer, 1023, cachefp);
		p = strrchr(buffer, '');
		if (p) {
			p++;
			p2 = strchr(p, '');
			if (p2) {
				*p2 = '\0';
				printf("%s", p);
			} else {
				printf("%s", p);
				while (fgets(buffer, 1023, cachefp)) {
					p2 = strchr(buffer, '');
					if (p2) {
						*p2 = '\0';
						if (strlen(buffer) > 0)
							printf("%s",
							       buffer);
						break;
					} else
						printf("%s", buffer);
				}
			}
		}
	}
	fflush(NULL);

	pthread_mutex_unlock(&mymutex);
}

//cache opschonen
void cleancache(void)
{
	char *p, *p2;
	char buffer[1024];
	char jaar[5], maand[3], dag[3];
	rewind(cachefp);
	while (fgets(buffer, 1023, cachefp)) {
		if (buffer[0] == '') {
			if ((p = strchr(buffer, '-'))) {
				p++;
				p2 = strchr(p, '');
				if (!p2)
					continue;
			} else
				continue;
		} else
			continue;
		strncpy(jaar, p, 4);
		jaar[4] = '\0';
		strncpy(maand, p + 4, 2);
		maand[2] = '\0';
		strncpy(dag, p + 6, 2);
		dag[2] = '\0';

		if (sys_jaar > atoi(jaar) || sys_maand > atoi(maand)) {
			//fprintf(stderr, "jaarcache: %s\n", buffer);
			//buffer entry overslaan        
			while (fgets(buffer, 1023, cachefp)) {
				if (strchr(buffer, ''))
					break;
			}
		} else if (sys_dag > atoi(dag)) {
			//fprintf(stderr, "dagcache: %s\n", buffer);
			//buffer entry overslaan        
			while (fgets(buffer, 1023, cachefp)) {
				if (strchr(buffer, ''))
					break;
			}
		} else {
			//fprintf(stderr, "cache: %s\n", buffer);
			fprintf(cachetmp, "%s", buffer);
			while (fgets(buffer, 1023, cachefp)) {
				fprintf(cachetmp, "%s", buffer);
				if (strchr(buffer, ''))
					break;
			}
		}
	}
}
